home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / sync.c < prev    next >
C/C++ Source or Header  |  1994-02-09  |  2KB  |  124 lines

  1. /*
  2.  * FILE
  3.  *    sync.c
  4.  *    
  5.  *    
  6.  * DESCRIPTION
  7.  *    syncing filesystems, makes only sense with mint and
  8.  *    minixfs for now
  9.  *    
  10.  * BUGS
  11.  *    minixfs V 060 PL 5 always syncs all drives, so there will
  12.  *    be too much syncing, since we call Dcntl for all known drives.
  13.  *    
  14.  */
  15.  
  16. #include <mintbind.h>
  17. #include <stat.h>
  18. #include <errno.h>
  19. #include <support.h>
  20.  
  21. extern int __mint;
  22.  
  23. /* from minixfs.h by S N Henson*/
  24. #define MFS_BASE    0x100
  25. #define MFS_VERIFY    (MFS_BASE)        /* Return minixfs magic number */
  26. #define MFS_SYNC    (MFS_BASE|0x01)    /* Sync the filesystem */
  27. #define MFS_MAGIC    0x18970431        /* Magic number from MFS_VERIFY */
  28.  
  29. /*
  30.  * FUNCTION
  31.  *    int sync(void)
  32.  *    
  33.  * DESCRIPTION
  34.  *    query all known drives for a valid MinixFs
  35.  *    if we find one, sync it.
  36.  */
  37. int sync(void)
  38. {
  39.     long            magic;
  40.     unsigned long    drives;
  41.     int                i, rv;
  42.     char            path[] = "A:\\";
  43.     
  44.     if (!__mint)
  45.         return 0;
  46.  
  47.     drives = Dsetdrv(Dgetdrv());
  48.  
  49.     drives &= ~0x3;    /* don't sync the floppys */
  50.     
  51.     for (i = 2; drives ; i++) {
  52.         if (drives & (1L << i)) {
  53.             drives &= ~(1L << i);
  54.             path[0] = 'A' + i;
  55.             magic = 0L;
  56.             if (!Dcntl(MFS_VERIFY, path, &magic) && magic == MFS_MAGIC) {
  57.                 if ((rv = (int)Dcntl(MFS_SYNC, path, 0L)) < 0) {
  58.                     errno = -rv;
  59.                     return -1;
  60.                 }
  61.             }
  62.         }
  63.     }
  64.         
  65.     return 0;
  66. }  /* sync() */
  67.  
  68.  
  69.  
  70. /*
  71.  * FUNCTION
  72.  *    int fsync(int fd)
  73.  *    
  74.  * DESCRIPTION
  75.  *    sync all buffers related to file descriptor fd
  76.  *    since MFS 605 always syncs all the buffers, we don't bother
  77.  *    to get the full path.
  78.  */
  79. int fsync(fd)
  80.     int    fd;
  81. {
  82.     int            rv;
  83.     long        magic     = 0L;
  84.     char        path[]    = "A:\\";
  85.     struct stat    statbuf;
  86.  
  87.     if (!__mint)
  88.         return 0;
  89.     
  90.     if (fstat(fd, &statbuf))
  91.         return -1;            /* errno set from fstat */
  92.  
  93.     if (statbuf.st_dev >= 32)
  94.       /* If mounted via FS_MOUNT, st_dev will be > 0x100.
  95.          Pretend that it worked. */
  96.       return 0;
  97.  
  98.     path[0] = 'A'+ statbuf.st_dev;
  99.     if (!Dcntl(MFS_VERIFY, path, &magic) && magic == MFS_MAGIC) {
  100.         if ((rv = (int)Dcntl(MFS_SYNC, path, 0L)) < 0) {
  101.             errno = -rv;
  102.             return -1;
  103.         }
  104.     }
  105.  
  106.     return 0;
  107. }  /* fsync() */
  108.  
  109.  
  110. #ifdef TEST
  111.  
  112. /*
  113.  * Im not in the mood to write a tricky test routine,
  114.  * so just do 'cat junk1 >junk2;sync' from your shell
  115.  * and listen to your harddisk.
  116.  */
  117. int main (void)
  118. {
  119.     sync();
  120.     return 0;
  121. }
  122.  
  123. #endif
  124.